﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace DAL.DBHelpers
{
    public class FtpImageDetail
    {
        public string url { get; set; }
        public DateTime date { get; set; }
    }
    public class UploadImage
    {
        public static string SERVER_PATH = "ftp://118.70.28.247/";  
        public static string UpLoad(Stream fileData, string fileName)
        {
            try
            {
                
                string path = SERVER_PATH + fileName;
                char i = '/';//i+1=0
                while (checkFileExist(path))
                {
                    string name = fileName.Substring(0, fileName.LastIndexOf('.'));
                    string exten = fileName.Substring(fileName.LastIndexOf('.'));
                    i++;
                    name += i;
                    fileName = name + exten;
                    path = SERVER_PATH + fileName;
                }
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path);
                request.Method = WebRequestMethods.Ftp.UploadFile;

                // This example assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential("tungnt", "tungnt1122");

                // Copy the contents of the file to the request stream.
                byte[] fileContents = new byte[(int)fileData.Length];
                int len = fileData.Read(fileContents, 0, (int)fileData.Length);
                request.ContentLength = len;

                Stream requestStream = request.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();

                response.Close();
                return fileName;
            }
            catch
            {
                return null;
            }
        }
        public static List<FtpImageDetail> getListFiles(string folder)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(SERVER_PATH + folder);
            request.Credentials = new NetworkCredential("tungnt", "tungnt1122");
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            try
            {
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(ftpStream, Encoding.UTF8);
                List<FtpImageDetail> result = new List<FtpImageDetail>();
                while (!reader.EndOfStream)
                {
                    string temp=reader.ReadLine().ToString();
                    FtpImageDetail fd = new FtpImageDetail();
                    fd.url = temp;
                    fd.date = getDate(fd.url);
                    result.Add(fd);
                }

                response.Close();
                ftpStream.Close();
                reader.Close();
                return result.OrderByDescending(x=>x.date).ToList();
            }
            catch
            {
                return null;
            }
        }

        private static DateTime getDate(string p)
        {
            DateTime result = DateTime.MinValue;
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(SERVER_PATH + p);
                request.Credentials = new NetworkCredential("tungnt", "tungnt1122");
                request.Method = WebRequestMethods.Ftp.GetDateTimestamp;
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                result = response.LastModified;
                response.Close();

            }
            catch
            {

            }
            return result;
        }
        //true neu file da ton tai
        private static bool checkFileExist(string fullPath)
        {
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(fullPath);
            request.Credentials = new NetworkCredential("tungnt", "tungnt1122");
            request.Method = WebRequestMethods.Ftp.GetFileSize;
            try
            {
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                response.Close();
                return true;
            }
            catch (WebException ex)
            {
                //File doesn't exists
                return false;
            }
        }
    }
}